<?php
namespace app\patrol\controller;
use app\patrol\logic\IndexLogic;

/**
 * Created by PhpStorm.
 * User: tyll
 * Date: 2019/09/06
 * Time: 11:55
 */
class Index extends Base {

    public function __construct(){
        parent::__construct();
        $this->i_logic = new IndexLogic();
    }

    /**
     * 获取社区巡店分部
     * @return \think\response\Json
     */
    public function ajax_get_cp_branch(){
        $params = input();
        $r = $this->check_user_token($params);
        if($r['code'] != 1){
            return json($r);
        }

        //检查用户规则
        $r = $this->i_logic->check_user_rule();
        if($r['code'] != 1){
            return json($r);
        }
        $u_detail = $r['data']['detail'];

        //检查社区巡店分部规则
        $r = $this->i_logic->check_cp_branch_rule($u_detail['use_branchid']);
        if($r['code'] != 1){
            return json($r);
        }
        $cp_branch_info = $r['data']['cp_branch_info'];

        //计算社区巡店开始时间和结束时间
        $cp = new \app\common\controller\Cp();
        $cp_time = $cp->count_cp_time();

        $cp_branch_info['start_time'] = $cp_time['start_time'];
        $cp_branch_info['end_time'] = $cp_time['end_time'];

        //获取常用巡店分部
        $maps = [];
        $maps[] = ['id','=',$u_detail['use_branchid']];
        $b_detail = model('branch')->get_detail($maps,'id,name');
        $cp_branch_info['branch_zw'] = $b_detail['name'];

        //计算用户常用分部今日完成次数
        $today_time = get_today_time();
        $start_date = date('Y-m-d',strtotime($today_time['start_time']));

        $maps = [];
        $maps[] = ['branchid','=',$u_detail['use_branchid']];
        $maps[] = ['time_code','<>','other'];
        $maps[] = ['now_date','=',$start_date];
        $maps[] = ['status','=',1];
        $cp_num = model('cp_branch')->get_count($maps,'id');
        $cp_branch_info['cp_notice'] = $b_detail['name'] . '-今日完成社区巡店次数（' . $cp_num . '/3）';

        $data['cp_branch_info'] = $cp_branch_info;
        return json($this->i_logic->get_response(1,'OK',$data));
    }
}
